package com.yz.android_baozou.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.yz.android_baozou.R;
import com.yz.android_baozou.adapter.Home_Video_RecyclerAdapter;
import com.yz.android_baozou.bean.VideoBean;
import com.yz.android_baozou.decoration.SpacesItemDecoration;
import com.yz.android_baozou.helper.OkhttpHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static android.content.ContentValues.TAG;

/**
 * Created by Administrator on 2016/11/1.
 */

public class Video_Home_Fragment extends Fragment {
    public String PATH_VIDEO = "http://dailyapi.ibaozou.com/api/v31/documents/videos/latest";
    @Bind(R.id.recyclerView_video_home)
    RecyclerView recyclerViewVideoHome;
    @Bind(R.id.scoreView_video)
    PullToRefreshScrollView scoreViewVideo;
    //timestamp title image(首页的图片) file_url(视频网址)
    private Context mContext = null;
    private List<VideoBean.DataBean> mList_VideoBean = new ArrayList<>();
    private Home_Video_RecyclerAdapter recyclerAdapter;
    private boolean flag;
    private String oldchar;
    private VideoBean videoBean;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_video_home, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //加载网络数据
        loadDataFromNet();
        //设置滚动监听
        scoreListener();
    }

    private void scoreListener() {
        //设置刷新模式
        scoreViewVideo.setMode(PullToRefreshBase.Mode.BOTH);
        //设置滚动监听
        scoreViewVideo.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                flag = true;
                PATH_VIDEO = "http://dailyapi.ibaozou.com/api/v31/documents/videos/latest";
                loadDataFromNet();
            }

            @Override
            //上拉刷新拼接字符串进行访问
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                flag = false;
                //判断是否是第一次上拉
                if (PATH_VIDEO.equals("http://dailyapi.ibaozou.com/api/v31/documents/videos/latest")) {
                    if (videoBean != null) {
                        //如果是 则拼接网址
                        PATH_VIDEO = PATH_VIDEO + "?timestamp=" + videoBean.getTimestamp() + "&";
                        //记录拼接的字符串
                        oldchar = videoBean.getTimestamp() + "";
                        loadDataFromNet();
                    }
                } else {
                    //当不是第一次进行上拉时 替换上一次的字符串
                    PATH_VIDEO = PATH_VIDEO.replace(oldchar, videoBean.getTimestamp() + "");
                    oldchar = videoBean.getTimestamp() + "";
                    loadDataFromNet();
                }
            }
        });
    }

    private void loadDataFromNet() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String json = OkhttpHelper.getStringFromUrl(PATH_VIDEO);
                videoBean = recyclerAdapter.parseJsonToBean(json);
                if (videoBean != null) {
                    mList_VideoBean = videoBean.getData();
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (flag) {
                                recyclerAdapter.reloadRecyclerView(mList_VideoBean, true);
                            } else {
                                recyclerAdapter.reloadRecyclerView(mList_VideoBean, flag);
                            }
                                if(scoreViewVideo!=null){
                                    scoreViewVideo.onRefreshComplete();
                                }
                        }
                    });
                }

            }
        }).start();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.i(TAG, "onActivityCreated: 预加载第3页了------>>>");
        //设置RecyclerView的中间的间距
        recyclerViewVideoHome.addItemDecoration(new SpacesItemDecoration(10));
        //使用线性布局管理器
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        //设置RecyclerView的布局管理器
        recyclerViewVideoHome.setLayoutManager(manager);
        //设置RecyclerView固定行高
        recyclerViewVideoHome.setHasFixedSize(true);
        //创建适配器
        recyclerAdapter = new Home_Video_RecyclerAdapter(mContext, mList_VideoBean);
        //设置RecyclerView的适配器
        recyclerViewVideoHome.setAdapter(recyclerAdapter);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
